package com.tin.triper.UI;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.tin.triper.Constants;
import com.tin.triper.R;
import com.tin.triper.Data.LocationProvider;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ResourcesListActivity extends ListActivity {
	SharedPreferences prefs;
	private ResourceAdapter adapter;
	private ArrayList<Resource> list = new ArrayList<Resource>();
	private List<TripPoint> pointList = new ArrayList<TripPoint>();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resource_list);
		prefs = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
		adapter = new ResourceAdapter(ResourcesListActivity.this,
				R.layout.resource_list_row, list);
		setListAdapter(adapter);
		loadList();
		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				final CharSequence[] items = { getString(R.string.common_edit),
						getString(R.string.common_erase) };
				AlertDialog.Builder builder = new AlertDialog.Builder(
						ResourcesListActivity.this);
				builder.setItems(items, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						if (item == 0) {
							Intent intent;
							switch (list.get(position).resourceType) {
							case 0:
								intent = new Intent(ResourcesListActivity.this,
										TextActivity.class);
								intent.putExtra(Constants.ACTIVITY_MODE, 1);
								intent.putExtra(Constants.TRIP_ID,
										list.get(position).tripId);
								intent.putExtra(
										Constants.TRIP_NAME,
										getIntent().getExtras().getString(
												Constants.TRIP_NAME));
								intent.putExtra(Constants.RESOURCE_ID,
										list.get(position).resourceId);
								startActivity(intent);
								break;
							case 1:
								intent = new Intent(ResourcesListActivity.this,
										VoiceActivity.class);
								intent.putExtra(Constants.ACTIVITY_MODE, 1);
								intent.putExtra(Constants.TRIP_ID,
										list.get(position).tripId);
								intent.putExtra(
										Constants.TRIP_NAME,
										getIntent().getExtras().getString(
												Constants.TRIP_NAME));
								intent.putExtra(Constants.RESOURCE_ID,
										list.get(position).resourceId);
								startActivity(intent);
								break;
							case 2:
								intent = new Intent(ResourcesListActivity.this,
										ImageActivity.class);
								intent.putExtra(Constants.ACTIVITY_MODE, 1);
								intent.putExtra(Constants.TRIP_ID,
										list.get(position).tripId);
								intent.putExtra(
										Constants.TRIP_NAME,
										getIntent().getExtras().getString(
												Constants.TRIP_NAME));
								intent.putExtra(Constants.RESOURCE_ID,
										list.get(position).resourceId);
								startActivity(intent);
								break;
							case 3:
								intent = new Intent(ResourcesListActivity.this,
										VideoActivity.class);
								intent.putExtra(Constants.ACTIVITY_MODE, 1);
								intent.putExtra(Constants.TRIP_ID,
										list.get(position).tripId);
								intent.putExtra(
										Constants.TRIP_NAME,
										getIntent().getExtras().getString(
												Constants.TRIP_NAME));
								intent.putExtra(Constants.RESOURCE_ID,
										list.get(position).resourceId);
								startActivity(intent);
								break;
							default:
								break;
							}
						}
						if (item == 1) {
							getContentResolver().delete(
									LocationProvider.CONTENT_URI_RESOURCES,
									LocationProvider.RESOURCE_ID + " ='"
											+ list.get(position).resourceId
											+ "'", null);
							loadList();
						}

					}
				});
				builder.create().show();
			}

		});
	}

	private void loadList() {
		list.clear();
		Cursor cursor = getContentResolver()
				.query(LocationProvider.CONTENT_URI_RESOURCES,
						new String[] { LocationProvider.RESOURCE_ID,
								LocationProvider.RESOURCE_TYPE,
								LocationProvider.RESOURCE_DESCRIPTION,
								LocationProvider.RESOURCE_FILENAME,
								LocationProvider.LOCATION_ID,
								LocationProvider.TRIP_ID },
						LocationProvider.TRIP_ID
								+ " ='"
								+ getIntent().getExtras().getInt(
										Constants.RESOURCE_TRIP_ID) + "'",
						null, null);
		if (cursor != null)
			while (cursor.moveToNext()) {
				Resource resource = new Resource();
				resource.resourceId = cursor.getInt(0);
				resource.resourceType = cursor.getInt(1);
				resource.description = cursor.getString(2);
				resource.filename = cursor.getString(3);
				resource.locationId = cursor.getInt(4);
				resource.tripId = cursor.getInt(5);
				list.add(resource);
			}
		cursor.close();
		for (int i = 0; i < list.size(); i++) {
			cursor = getContentResolver().query(
					LocationProvider.CONTENT_URI_LOCATION,
					new String[] { LocationProvider.LOCATION_TIME },
					LocationProvider.LOCATION_ID + " ='"
							+ list.get(i).locationId + "'", null, null);
			if (cursor != null)
				if (cursor.moveToFirst()) {
					list.get(i).date.setTime(cursor.getLong(0));
				}
			cursor.close();
		}
		adapter.notifyDataSetChanged();
	}

	private class ResourceAdapter extends ArrayAdapter<Resource> implements
			Filterable {

		public ArrayList<Resource> allItems;
		LayoutInflater layoutInflator;

		public ResourceAdapter(Context context, int textViewResourceId,
				ArrayList<Resource> items) {
			super(context, textViewResourceId, items);
			allItems = items;
		}

		class FeedViewHolder {
			TextView description;
			TextView resourceType;
			TextView resourceDate;

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			FeedViewHolder feedViewHolder;

			TextView description;
			TextView resourceType;
			TextView resourceDate;

			final Resource o;

			if (allItems.size() != 0)
				o = allItems.get(position);
			else
				o = null;

			if (convertView == null) {
				layoutInflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = layoutInflator.inflate(
						R.layout.resource_list_row, parent, false);
				feedViewHolder = new FeedViewHolder();
				feedViewHolder.description = (TextView) convertView
						.findViewById(R.id.resource_list_description);
				feedViewHolder.resourceType = (TextView) convertView
						.findViewById(R.id.resource_list_resource_type);
				feedViewHolder.resourceDate = (TextView) convertView
						.findViewById(R.id.resource_list_resource_time);

				convertView.setTag(feedViewHolder);
				description = feedViewHolder.description;
				resourceType = feedViewHolder.resourceType;
				resourceDate = feedViewHolder.resourceDate;
			} else {
				feedViewHolder = (FeedViewHolder) convertView.getTag();
				description = feedViewHolder.description;
				resourceType = feedViewHolder.resourceType;
				resourceDate = feedViewHolder.resourceDate;
			}
			if (o != null) {
				SimpleDateFormat formatter = new SimpleDateFormat(
						"yyyy.MM.dd 'at' hh:mm:ss ");
				resourceDate.setText(getString(R.string.trip_list_date) + ": "
						+ formatter.format(o.date));

				if (o.description.length() > 15)
					description.setText(o.description.substring(0, 15) + "...");
				else
					description.setText(o.description);
				switch (o.resourceType) {
				case 0:
					resourceType.setText(getString(R.string.common_type) + ": "
							+ getString(R.string.common_text));
					break;
				case 1:
					resourceType.setText(getString(R.string.common_type) + ": "
							+ getString(R.string.common_voice));
					break;
				case 2:
					resourceType.setText(getString(R.string.common_type) + ": "
							+ getString(R.string.common_photo));
					break;
				case 3:
					resourceType.setText(getString(R.string.common_type) + ": "
							+ getString(R.string.common_video));
					break;
				}
			}
			convertView.setTag(feedViewHolder);
			return convertView;
		}
	}

	private class Resource {
		public int resourceId;
		public int tripId;
		public int resourceType;
		public String description;
		public String filename;
		public int locationId;
		public Date date = new Date();
	}

	private class TripPoint extends GeoPoint {

		private int time;
		private int altitude;

		public TripPoint(int latitudeE6, int longitudeE6) {
			super(latitudeE6, longitudeE6);
			// TODO Auto-generated constructor stub
		}

		public TripPoint(int latitudeE6, int longitudeE6, int time, int altitude) {
			super(latitudeE6, longitudeE6);
			this.time = time;
		}

	}
}
